package com.wbao.dianniu.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.listener.IFriendRemarkListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.FriendRemarkManager;
import com.wbao.dianniu.manager.ModifyRemarkHelper;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.Utils;

/**
 * 修改备注
 *
 * @author yejianjun
 */
public class ModifyRemarkActivity extends BaseActivity implements IFriendRemarkListener {

    private EditText mEditText;
    private ImageButton deletedBtn;
    private Button rightBtn;
    private FriendRemarkManager manager;
    private int friendAccountId;
    private final int MAX_COUNT = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.modify_remark);
        setTitleName("修改备注");
        rightBtn = getRightTextButton();
        rightBtn.setText("确认");
        rightBtn.setOnClickListener(this);
        mEditText = (EditText) findViewById(R.id.remarkEdit);
        deletedBtn = (ImageButton) findViewById(R.id.remarkDelete);
        deletedBtn.setOnClickListener(this);
        manager = new FriendRemarkManager(this);
        manager.addFriendRemarkListener(this);
        friendAccountId = getIntent().getIntExtra(Const.BUNDLE_ACCOUNTID, 0);
        String remark = getIntent().getStringExtra(Const.BUNDLE_REALNAME);
        mEditText.setText(remark == null ? "" : remark);
        Utils.ContentLengthLimit(this, mEditText, MAX_COUNT);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.remarkDelete:
                mEditText.setText("");
                break;
            case R.id.right_button:
                String name = mEditText.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    Notification.toast(ModifyRemarkActivity.this, "昵称不能为空");
                    return;
                }
                manager.friendRemark(GlobalContext.getAccountId(), friendAccountId, mEditText.getText().toString());
                break;
            default:
                break;
        }

    }

    @Override
    public void onFriendRemarkFailure(int errorCode, String errorMsg) {
    }

    @Override
    public void onFriendRemarkSuccess() {
        ModifyRemarkHelper.getInstance().remarkChanged(mEditText.getText().toString());
        finish();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeFriendRemarkListener(this);
        }
    }
}
